<template>
  <div class>
    <div class="card tr mb2">
      <el-tooltip content="显示字段筛选" placement="bottom" effect="light">
        <el-popover placement="bottom" width="200" trigger="click">
          <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
          <el-checkbox-group v-model="showItemList" @change="handleCheckedChange">
            <el-checkbox v-for="(item,key) in allItemList" :key="key" :label="item">{{item.name}}</el-checkbox>
          </el-checkbox-group>
          <el-button slot="reference" icon="el-icon-menu" size="mini" type="primary" circle></el-button>
        </el-popover>
      </el-tooltip>
      <el-tooltip content="展开/收起表单" placement="bottom" effect="light">
        <el-button icon="el-icon-search" @click="formShow= !formShow" size="mini" type="primary" circle></el-button>
      </el-tooltip>
      <el-button size="mini" type="primary" icon="el-icon-plus" @click="showDialog">添加</el-button>
    </div>
    <div class="card" v-show="formShow">
      <el-form size="mini" :style="style" :model="ruleForm" ref="ruleForm" inline label-width="100px" class="demo-ruleForm">
        <my-form-item label="桌面名称" prop="title">
          <el-input size="mini" v-model="ruleForm.title"></el-input>
        </my-form-item>
        <my-form-item>
          <el-button size="mini" type="primary" @click="submitForm('ruleForm')">查询</el-button>
          <el-button size="mini" @click="resetForm('ruleForm')">重置</el-button>
        </my-form-item>
      </el-form>
    </div>
    <el-table stripe ref="dragTable" :data="tableData" border style="width: 100%">
      <el-table-column type="index" width="60px" align="center" fixed="left" label="序号"></el-table-column>
      <el-table-column v-for="(item,index) in allItemList" :width="item.width || '' " :key="index" :label="item.name" v-if="item.show">
        <template slot-scope="scope">
          <template v-if="!item.formatter">{{ scope.row[item.key] }}</template>
          <template v-else>
            <span v-text="item.formatter(scope.row, item)"></span>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180px" fixed="right" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="showEditDialog(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-card style="text-align:right;">
      <el-pagination @size-change="sizeChange" @current-change="currentChange" :current-page="currentPage" :page-sizes="[ 10, 20,30, 40]" :page-size="pageSize" background layout="total, sizes, prev, pager, next" :total="total"></el-pagination>
    </el-card>
    <!-- --------------------------------------------------------添加弹出框 -->
    <el-dialog title="新增管理" width="300px" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
      <el-form size="mini" :style="style" :model="addForm" ref="addForm" :rules="rules" label-width="100px" class="demo-ruleForm">
        <my-form-item label="桌面名称" :label-width="formLabelWidth" prop="title">
          <el-input size="mini" v-model="addForm.title"></el-input>
        </my-form-item>
        <my-form-item label="桌面编码" :label-width="formLabelWidth" prop="code">
          <el-input size="mini" v-model="addForm.code"></el-input>
        </my-form-item>
        <my-form-item label="X轴" :label-width="formLabelWidth" prop="xAxis">
          <el-input size="mini" v-model="addForm.xAxis"></el-input>
        </my-form-item>
        <my-form-item label="Y轴" :label-width="formLabelWidth" prop="yAxis">
          <el-input size="mini" v-model="addForm.yAxis"></el-input>
        </my-form-item>
        <my-form-item label="高度" :label-width="formLabelWidth" prop="hAxis">
          <el-input size="mini" v-model="addForm.hAxis"></el-input>
        </my-form-item>
        <my-form-item label="宽度" :label-width="formLabelWidth" prop="wAxis">
          <el-input size="mini" v-model="addForm.wAxis"></el-input>
        </my-form-item>
        <my-form-item label="组件名称" :label-width="formLabelWidth" prop="zjName">
          <el-input size="mini" v-model="addForm.zjName"></el-input>
        </my-form-item>
        <my-form-item label="组件图表" :label-width="formLabelWidth" prop="zjIcon">
          <el-input size="mini" v-model="addForm.zjIcon"></el-input>
        </my-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogFormVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="handleAdd()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- --------------------------------------------------------编辑弹出框 -->
    <el-dialog title="编辑管理" width="300px" :visible.sync="dialogEditVisible" :close-on-click-modal="false">
      <el-form size="mini" :style="style" :model="editForm" ref="editForm" :rules="rules" label-width="100px" class="demo-ruleForm">
        <my-form-item label="桌面名称" :label-width="formLabelWidth" prop="title">
          <el-input size="mini" v-model="editForm.title"></el-input>
        </my-form-item>
        <my-form-item label="桌面编码" :label-width="formLabelWidth" prop="code">
          <el-input size="mini" v-model="editForm.code"></el-input>
        </my-form-item>
        <my-form-item label="X轴" :label-width="formLabelWidth" prop="xAxis">
          <el-input size="mini" v-model="editForm.xAxis"></el-input>
        </my-form-item>
        <my-form-item label="Y轴" :label-width="formLabelWidth" prop="yAxis">
          <el-input size="mini" v-model="editForm.yAxis"></el-input>
        </my-form-item>
        <my-form-item label="高度" :label-width="formLabelWidth" prop="hAxis">
          <el-input size="mini" v-model="editForm.hAxis"></el-input>
        </my-form-item>
        <my-form-item label="宽度" :label-width="formLabelWidth" prop="wAxis">
          <el-input size="mini" v-model="editForm.wAxis"></el-input>
        </my-form-item>
        <my-form-item label="组件名称" :label-width="formLabelWidth" prop="zjName">
          <el-input size="mini" v-model="editForm.zjName"></el-input>
        </my-form-item>
        <my-form-item label="组件图标" :label-width="formLabelWidth" prop="zjIcon">
          <el-input size="mini" v-model="editForm.zjIcon"></el-input>
        </my-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogEditVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="handleEdit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
const defaultFormThead = [
  { key: "title", name: "桌面名称",show:true },
  { key: "code", name: "桌面编码",show:true },
  { key: "xAxis", name: "X轴",show:true},
  { key: "yAxis", name: "Y轴",show:true },
  { key: "hAxis", name: "高度",show:true },
  { key: "wAxis", name: "宽度",show:true },
  { key: "zjName", name: "组件名称",show:true }
];
import EnumSelector from "@/pages/comm/enumSelector";
export default {
  components: { EnumSelector },
  data() {
    return {
      checkAll: true,
      isIndeterminate: false,
      allItemList: defaultFormThead,
      showItemList: defaultFormThead,
      formShow: false,
      dialogFormVisible: false,
      dialogEditVisible: false,
      offForm: true, //是否折叠
      folding: false, //表单是否超过一行
      formLabelWidth: "80px",
      tableData: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      ruleForm: {
        title: ""
      },
      style: {},
      rules: {
        title: [
          {
            required: true,
            message: "请输入桌面名称",
            trigger: "blur"
          }
        ],
        code: [
          {
            required: true,
            message: "请输入桌面编码",
            trigger: "blur"
          }
        ],
        xAxis: [
          {
            required: true,
            message: "请输入X轴坐标",
            trigger: "blur"
          }
        ],
        yAxis: [
          {
            required: true,
            message: "请输入X轴坐标",
            trigger: "blur"
          }
        ],
        hAxis: [
          {
            required: true,
            message: "请输入高度坐标",
            trigger: "blur"
          }
        ],
        wAxis: [
          {
            required: true,
            message: "请输入宽度坐标",
            trigger: "blur"
          }
        ],
        zjName: [
          {
            required: true,
            message: "请输入组件名称",
            trigger: "blur"
          }
        ],
        zjIcon: [
          {
            required: true,
            message: "请输入组件图表",
            trigger: "blur"
          }
        ]
      },
      addForm: {
        title: "",
        code: "",
        xAxis: "",
        yAxis: "",
        hAxis: "",
        wAxis: "",
        zjName: "",
        zjIcon: ""
      },
      editForm: {
        title: "",
        code: "",
        xAxis: "",
        yAxis: "",
        hAxis: "",
        wAxis: "",
        zjName: "",
        zjIcon: ""
      }
    };
  },

  mounted() {
    this.init();
    let formHeight = this.$refs.ruleForm.$el.clientHeight;
    if (formHeight > 62) {
      this.folding = true;
      this.style = {
        height: "62px",
       //overflow: "hidden"
      };
    } else {
      this.folding = false;
      this.style = {
        height: "auto",
        //overflow: "hidden"
      };
    }
  },
  computed: {},
  watch: {
    offForm: function(v) {
      if (v) {
        this.style = {
          height: "62px",
          //overflow: "hidden"
        };
      } else {
        this.style = {
          height: "auto",
          //overflow: "hidden"
        };
      }
    },
    showItemList(valArr) {
        for (let key in this.allItemList) {
          this.allItemList[key].show = false;
          for (let key1 in valArr) {
            if (this.allItemList[key].key == valArr[key1].key) {
              this.allItemList[key].show = true;
            }
          }
        }
      this.$nextTick(() => {
        this.$refs.dragTable.doLayout();
      });
    }
  },
  methods: {
    handleDownload() {
      this.downloadLoading = true;
      import("@/vendor/Export2Excel").then(excel => {
        const tHeader = [
          "id",
          "桌面名称",
          "桌面编码",
          "X轴",
          "Y轴",
          "高度",
          "宽度",
          "组件名称",
          "zjIcon"
        ];
        const filterVal = [
          "id",
          "title",
          "code",
          "xAxis",
          "yAxis",
          "hAxis",
          "wAxis",
          "zjName",
          "zjIcon"
        ];
        const data = this.formatJson(filterVal, this.tableData);
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: "table-list"
        });
        this.downloadLoading = false;
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v =>
        filterVal.map(j => {
          if (j === "timestamp") {
            return parseTime(v[j]);
          } else {
            return v[j];
          }
        })
      );
    },
    handleCheckedChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === defaultFormThead.length;
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < defaultFormThead.length;
    },
    handleCheckAllChange(val) {
      this.showItemList = val ? defaultFormThead : [];
      this.isIndeterminate = false;
    },
    sizeChange(n) {
      this.pageSize = n;
      this.init();
    },
    currentChange(n) {
      this.currentPage = n;
      this.init();
    },
    init() {
      let searchForm = {};
      searchForm.title = this.ruleForm.title;
      let params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        ...searchForm
      };
      this.$api
        .formPost("/api/biz/potal/potSet/doGetList", params)
        .then(res => {
          if (res.retHead.code == "0000000") {
            this.tableData = res.retBody.list;
            this.total = res.retBody.total;
            this.currentPage = res.retBody.pageNum;
          }
        });
    },
    showDialog() {
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.resetForm("addForm");
      });
      this.form = {};
    },
    showEditDialog(index, row) {
      if (row.id) {
        this.editForm.id = row.id;
      }
      if (row.title) {
        this.editForm.title = row.title;
      }
      if (row.code) {
        this.editForm.code = row.code;
      }
      if (row.xAxis) {
        this.editForm.xAxis = row.xAxis;
      }
      if (row.yAxis) {
        this.editForm.yAxis = row.yAxis;
      }
      if (row.hAxis) {
        this.editForm.hAxis = row.hAxis;
      }
      if (row.wAxis) {
        this.editForm.wAxis = row.wAxis;
      }
      if (row.zjName) {
        this.editForm.zjName = row.zjName;
      }
      if (row.zjIcon) {
        this.editForm.zjIcon = row.zjIcon;
      }
      this.dialogEditVisible = true;
    },
    handleAdd() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          let addFormParams = {};
          addFormParams.id = this.addForm.id;
          addFormParams.title = this.addForm.title;
          addFormParams.code = this.addForm.code;
          addFormParams.xAxis = this.addForm.xAxis;
          addFormParams.yAxis = this.addForm.yAxis;
          addFormParams.hAxis = this.addForm.hAxis;
          addFormParams.wAxis = this.addForm.wAxis;
          addFormParams.zjName = this.addForm.zjName;
          addFormParams.zjIcon = this.addForm.zjIcon;
          this.$api
            .formPost("/api/biz/potal/potSet/doSave", addFormParams)
            .then(res => {
              if (res.retHead.code == "0000000") {
                this.$message({
                  message: "添加成功！",
                  type: "success"
                });
                this.dialogFormVisible = false;
                this.init();
              } else {
                this.$message({
                  message: "添加失败！",
                  type: "error"
                });
              }
            });
        } else {
          this.$message.error("请完善表单");
          return false;
        }
      });
    },
    handleEdit() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          let editFormParams = {};
          editFormParams.id = this.editForm.id;
          editFormParams.title = this.editForm.title;
          editFormParams.code = this.editForm.code;
          editFormParams.xAxis = this.editForm.xAxis;
          editFormParams.yAxis = this.editForm.yAxis;
          editFormParams.hAxis = this.editForm.hAxis;
          editFormParams.wAxis = this.editForm.wAxis;
          editFormParams.zjName = this.editForm.zjName;
          editFormParams.zjIcon = this.editForm.zjIcon;
          this.$api
            .formPost("/api/biz/potal/potSet/doModById", editFormParams)
            .then(res => {
              if (res.retHead.code == "0000000") {
                this.$message.success("执行成功！");
                this.init();
                this.dialogEditVisible = false;
              } else {
                this.$message.error(res.retHead.message);
              }
            });
        } else {
          this.$message.error("请完善表单");
          return false;
        }
      });
    },

    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$api
            .formPost("/api/biz/potal/potSet/doRmById", {
              id: row.id
            })
            .then(res => {
              if (res.retHead.code == "0000000") {
                this.$message({
                  message: "操作成功！",
                  type: "success"
                });
                this.init();
              } else {
                this.$message({
                  message: "操作失败！",
                  type: "error"
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    submitForm(formName) {
      this.init();
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {}
};
</script>
<style lang='scss' scoped>
.btns {
  text-align: right;
}
.icon-arrow {
  position: absolute;
  bottom: 5px;
  left: 0;
  width: 100%;
  height: 1px;
  background-color: #f1f1f1;
  i {
    background-color: #fff;
    position: absolute;
    color: #999;
    bottom: -7px;
    margin-left: 50%;
    transform: translateX(-50%);
    cursor: pointer;
  }
}
</style>